﻿using System;
using System.Collections.Generic;
using Steamworks;

namespace Terraria.Social.Steam
{
	// Token: 0x02000134 RID: 308
	public class Lobby
	{
		// Token: 0x06000D43 RID: 3395 RVA: 0x003F7468 File Offset: 0x003F5668
		public Lobby()
		{
			this._lobbyEnter = CallResult<LobbyEnter_t>.Create(new CallResult<LobbyEnter_t>.APIDispatchDelegate(this.OnLobbyEntered));
			this._lobbyCreated = CallResult<LobbyCreated_t>.Create(new CallResult<LobbyCreated_t>.APIDispatchDelegate(this.OnLobbyCreated));
		}

		// Token: 0x06000D44 RID: 3396 RVA: 0x003F74DC File Offset: 0x003F56DC
		public void Create(bool inviteOnly, CallResult<LobbyCreated_t>.APIDispatchDelegate callResult)
		{
			SteamAPICall_t steamAPICall_t = SteamMatchmaking.CreateLobby(inviteOnly ? 0 : 1, 256);
			this._lobbyCreatedExternalCallback = callResult;
			this._lobbyCreated.Set(steamAPICall_t, null);
			this.State = LobbyState.Creating;
		}

		// Token: 0x06000D45 RID: 3397 RVA: 0x0000D4E9 File Offset: 0x0000B6E9
		public void OpenInviteOverlay()
		{
			if (this.State == LobbyState.Inactive)
			{
				SteamFriends.ActivateGameOverlayInviteDialog(new CSteamID(Main.LobbyId));
				return;
			}
			SteamFriends.ActivateGameOverlayInviteDialog(this.Id);
		}

		// Token: 0x06000D46 RID: 3398 RVA: 0x003F7518 File Offset: 0x003F5718
		public void Join(CSteamID lobbyId, CallResult<LobbyEnter_t>.APIDispatchDelegate callResult)
		{
			if (this.State != LobbyState.Inactive)
			{
				return;
			}
			this.State = LobbyState.Connecting;
			this._lobbyEnterExternalCallback = callResult;
			SteamAPICall_t steamAPICall_t = SteamMatchmaking.JoinLobby(lobbyId);
			this._lobbyEnter.Set(steamAPICall_t, null);
		}

		// Token: 0x06000D47 RID: 3399 RVA: 0x003F7550 File Offset: 0x003F5750
		public byte[] GetMessage(int index)
		{
			CSteamID csteamID;
			EChatEntryType echatEntryType;
			int lobbyChatEntry = SteamMatchmaking.GetLobbyChatEntry(this.Id, index, ref csteamID, this._messageBuffer, this._messageBuffer.Length, ref echatEntryType);
			byte[] array = new byte[lobbyChatEntry];
			Array.Copy(this._messageBuffer, array, lobbyChatEntry);
			return array;
		}

		// Token: 0x06000D48 RID: 3400 RVA: 0x0000D50E File Offset: 0x0000B70E
		public int GetUserCount()
		{
			return SteamMatchmaking.GetNumLobbyMembers(this.Id);
		}

		// Token: 0x06000D49 RID: 3401 RVA: 0x0000D51B File Offset: 0x0000B71B
		public CSteamID GetUserByIndex(int index)
		{
			return SteamMatchmaking.GetLobbyMemberByIndex(this.Id, index);
		}

		// Token: 0x06000D4A RID: 3402 RVA: 0x0000D529 File Offset: 0x0000B729
		public bool SendMessage(byte[] data)
		{
			return this.SendMessage(data, data.Length);
		}

		// Token: 0x06000D4B RID: 3403 RVA: 0x0000D535 File Offset: 0x0000B735
		public bool SendMessage(byte[] data, int length)
		{
			return this.State == LobbyState.Active && SteamMatchmaking.SendLobbyChatMsg(this.Id, data, length);
		}

		// Token: 0x06000D4C RID: 3404 RVA: 0x0000D54F File Offset: 0x0000B74F
		public void Set(CSteamID lobbyId)
		{
			this.Id = lobbyId;
			this.State = LobbyState.Active;
			this.Owner = SteamMatchmaking.GetLobbyOwner(lobbyId);
		}

		// Token: 0x06000D4D RID: 3405 RVA: 0x0000D56B File Offset: 0x0000B76B
		public void SetPlayedWith(CSteamID userId)
		{
			if (this._usersSeen.Contains(userId))
			{
				return;
			}
			SteamFriends.SetPlayedWith(userId);
			this._usersSeen.Add(userId);
		}

		// Token: 0x06000D4E RID: 3406 RVA: 0x0000D58F File Offset: 0x0000B78F
		public void Leave()
		{
			if (this.State == LobbyState.Active)
			{
				SteamMatchmaking.LeaveLobby(this.Id);
			}
			this.State = LobbyState.Inactive;
			this._usersSeen.Clear();
		}

		// Token: 0x06000D4F RID: 3407 RVA: 0x003F7594 File Offset: 0x003F5794
		private void OnLobbyEntered(LobbyEnter_t result, bool failure)
		{
			if (this.State != LobbyState.Connecting)
			{
				return;
			}
			if (failure)
			{
				this.State = LobbyState.Inactive;
			}
			else
			{
				this.State = LobbyState.Active;
			}
			this.Id = new CSteamID(result.m_ulSteamIDLobby);
			this.Owner = SteamMatchmaking.GetLobbyOwner(this.Id);
			this._lobbyEnterExternalCallback.Invoke(result, failure);
		}

		// Token: 0x06000D50 RID: 3408 RVA: 0x003F75F0 File Offset: 0x003F57F0
		private void OnLobbyCreated(LobbyCreated_t result, bool failure)
		{
			if (this.State != LobbyState.Creating)
			{
				return;
			}
			if (failure)
			{
				this.State = LobbyState.Inactive;
			}
			else
			{
				this.State = LobbyState.Active;
			}
			this.Id = new CSteamID(result.m_ulSteamIDLobby);
			this.Owner = SteamMatchmaking.GetLobbyOwner(this.Id);
			this._lobbyCreatedExternalCallback.Invoke(result, failure);
		}

		// Token: 0x040011BA RID: 4538
		private HashSet<CSteamID> _usersSeen = new HashSet<CSteamID>();

		// Token: 0x040011BB RID: 4539
		private byte[] _messageBuffer = new byte[1024];

		// Token: 0x040011BC RID: 4540
		public CSteamID Id = CSteamID.Nil;

		// Token: 0x040011BD RID: 4541
		public CSteamID Owner = CSteamID.Nil;

		// Token: 0x040011BE RID: 4542
		public LobbyState State;

		// Token: 0x040011BF RID: 4543
		private CallResult<LobbyEnter_t> _lobbyEnter;

		// Token: 0x040011C0 RID: 4544
		private CallResult<LobbyEnter_t>.APIDispatchDelegate _lobbyEnterExternalCallback;

		// Token: 0x040011C1 RID: 4545
		private CallResult<LobbyCreated_t> _lobbyCreated;

		// Token: 0x040011C2 RID: 4546
		private CallResult<LobbyCreated_t>.APIDispatchDelegate _lobbyCreatedExternalCallback;
	}
}
